#include "Checker.h"

Checker::Checker()
{
}

PropertyChecker::PropertyChecker()
	: Checker()
{
}

bool PropertyChecker::check(OperatorNode *node1, OperatorNode *node2)
{
	if (node1 == node2)
	{
		return false;
	}

	if (!checkCommutativity(node1, node2))
	{
		return false;
	}

	return true;
}

bool PropertyChecker::checkCommutativity(OperatorNode *node1, OperatorNode *node2)
{
	if (node1 == node2)
	{
		return false;
	}

	return node1->op->findCommOp(node2->op) || node2->op->findCommOp(node1->op);
}

bool PropertyChecker::checkDependencies(Query *query)
{
	if (query->node->dependencies)
	{
		for (Dependencies::iterator i = query->node->dependencies->begin(); i != query->node->dependencies->end(); ++i)
		{
			if (!checkDependencies(query, *i))
			{
				return false;
			}
		}
	}

	for (std::vector <Query *> :: iterator i = query->lists.begin(); i != query->lists.end(); ++i)
	{
		if (!checkDependencies(*i))
		{
			return false;
		}
	}

	return true;
}

bool PropertyChecker::checkDependencies(Query *query, OperatorNode *node)
{
	bool everyTree = true;

	for (std::vector <Query *> :: iterator i = query->lists.begin(); i != query->lists.end(); ++i)
	{
		if ((*i)->node == node)
		{
			return true;
		}

		if (!checkDependencies(*i, node))
		{
			everyTree = false;
		}
	}

	if (!query->lists.size() && everyTree)
	{
		everyTree = false;
	}

	return everyTree;
}

bool PropertyChecker::checkDistributivity(OperatorNode *node1, OperatorNode *node2)
{
	if (node1 == node2)
	{
		return false;
	}

	return (node1->op->checkProperty(PROPERTY_DISTRIBUTIVITY) || (node2->op->checkProperty(PROPERTY_DISTRIBUTIVITY)));
}

bool PropertyChecker::checkMovable(OperatorNode *node1, OperatorNode *node2)
{
	if (node1 == node2)
	{
		return false;
	}

	return (node1->op->checkProperty(PROPERTY_NOT_MOVABLE) || (node2->op->checkProperty(PROPERTY_NOT_MOVABLE)));
}